//
//  UserRacketCell.m
//  CoolTennisBall
//
//  Created by Coollang on 16/8/31.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "UserRacketCell.h"
#import "UserSeriesViewModel.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "NSDate+CM_DateTools.h"
#import "NSDate+FormateString.h"
#import "UIImageView+CM_ShowImage.h"

@interface UserRacketCell ()
@property (weak, nonatomic) IBOutlet UIView *RacketView;
@property (weak, nonatomic) IBOutlet UIImageView *racketImageView;
@property (weak, nonatomic) IBOutlet UILabel *racketBrandLabel;
@property (weak, nonatomic) IBOutlet UILabel *racketModelLabel;
@property (weak, nonatomic) IBOutlet UILabel *racketBuyDayLabel;

// 横线
@property (weak, nonatomic) IBOutlet UILabel *hLineLabel;
@property (weak, nonatomic) IBOutlet UILabel *hLineBrandLabel;
@property (weak, nonatomic) IBOutlet UILabel *hLineModleLabel;
@property (weak, nonatomic) IBOutlet UILabel *hLineSizeLabel;
@property (weak, nonatomic) IBOutlet UILabel *hLineTensionLabel;
@property (weak, nonatomic) IBOutlet UILabel *hLineCenterSizeLabel;
@property (weak, nonatomic) IBOutlet UILabel *hLineCenterTensionLabel;


// 竖线
@property (weak, nonatomic) IBOutlet UILabel *VLineLabel;
@property (weak, nonatomic) IBOutlet UILabel *VLineBrandLabel;
@property (weak, nonatomic) IBOutlet UILabel *VLineModleLabel;
@property (weak, nonatomic) IBOutlet UILabel *VLineSizeLabel;
@property (weak, nonatomic) IBOutlet UILabel *VLineTensionLabel;
@property (weak, nonatomic) IBOutlet UILabel *VLineCenterSizeLabel;
@property (weak, nonatomic) IBOutlet UILabel *VLineCenterTensionLabel;

@property (nonatomic, copy)NSString *VerticalLine; //竖线


@property (nonatomic, copy)NSString *vLineBrand; //品牌
@property (nonatomic, copy)NSString *vLineModel; // 类型
@property (nonatomic, copy)NSString *vLineSize;// 规格
@property (nonatomic, copy)NSString *vLineTenssion; // 张力

@property (nonatomic, copy)NSString *Line; //横线

@property (nonatomic, copy)NSString *hLineBrand; //品牌
@property (nonatomic, copy)NSString *hLineModel; // 类型
@property (nonatomic, copy)NSString *hLineSize;// 规格
@property (nonatomic, copy)NSString *hLineTenssion; // 张力

@end

@implementation UserRacketCell

- (void)setViewModle:(UserEquipModel *)viewModle {
    _viewModle = viewModle;
    self.VerticalLine = viewModle.VerticalLine;
    self.Line = viewModle.Line;
    
    [self.racketImageView sd_setImageWithURL:[NSURL URLWithString:viewModle.RacketUrl] placeholderImage:[UIImage imageNamed:@"球拍-未设置"] options:SDWebImageLowPriority |SDWebImageRetryFailed];
//    球拍
    if (viewModle.Brand.length <= 0 && viewModle.BuyTime.length <= 0) {
        
        self.racketModelLabel.text = NSLocalizedString(@"User.Not Set", @"未设置");
        self.racketModelLabel.font = [UIFont systemFontOfSize:16];
        self.racketModelLabel.textColor = [UIColor colorWithHex:0xaaaaaa];
        
    }else {
        self.racketModelLabel.font = [UIFont systemFontOfSize:14];
        self.racketModelLabel.textColor = [UIColor colorWithHex:0x151515];
        NSArray *brand = [viewModle.Brand componentsSeparatedByString:@","];
        self.racketBuyDayLabel.text = [self disposeRactetBuyTimeWith:viewModle.BuyTime];
       
        if (brand.count >= 2) {
            self.racketBrandLabel.text = brand[0];
            self.racketModelLabel.text = brand[1];
        }
        
    }
    
//    横线
    if (viewModle.VerticalLine.length <= 0) {
        self.hLineCenterSizeLabel.text = NSLocalizedString(@"User.Not Set", @"未设置");
        self.hLineCenterSizeLabel.font = [UIFont systemFontOfSize:16];
        self.hLineCenterSizeLabel.textColor = [UIColor colorWithHex:0xaaaaaa];
    }else if (self.hLineSize.length > 0 || self.hLineTenssion.length >0 ){
        
        self.hLineCenterSizeLabel.font = [UIFont systemFontOfSize:12];
        self.hLineCenterSizeLabel.textColor = [UIColor colorWithHex:0x787878];
        
        if (self.hLineBrand.length <= 0 && self.hLineModel.length <= 0) {
            self.hLineCenterSizeLabel.text = self.hLineSize;
            self.hLineCenterTensionLabel.text = self.hLineTenssion;
            
        }else {
            self.hLineCenterTensionLabel.text = @"";
            self.hLineCenterSizeLabel.text = @"";
            self.hLineBrandLabel.text = self.hLineBrand;
            self.hLineModleLabel.text = self.hLineModel;
            self.hLineSizeLabel.text = self.hLineSize;
            self.hLineTensionLabel.text = self.hLineTenssion;
        }
        
    }
//    竖线
    if (viewModle.Line.length <= 0) {
        self.VLineCenterSizeLabel.text = NSLocalizedString(@"User.Not Set", @"未设置");
        self.VLineCenterSizeLabel.font = [UIFont systemFontOfSize:16];
        self.VLineCenterSizeLabel.textColor = [UIColor colorWithHex:0xaaaaaa];
    }else if (self.vLineSize.length > 0 || self.vLineTenssion.length >0 ){
        
        self.VLineCenterSizeLabel.font = [UIFont systemFontOfSize:12];
        self.VLineCenterSizeLabel.textColor = [UIColor colorWithHex:0x787878];
        
        if (self.vLineBrand.length <= 0 && self.vLineModel.length <= 0) {
            self.VLineCenterSizeLabel.text = self.vLineSize;
            self.VLineCenterTensionLabel.text = self.vLineTenssion;
            
        }else {
            self.VLineCenterSizeLabel.text = @"";
            self.VLineCenterTensionLabel.text = @"";
            self.VLineBrandLabel.text = self.vLineBrand;
            self.VLineModleLabel.text = self.vLineModel;
            self.VLineSizeLabel.text = self.vLineSize;
            self.VLineTensionLabel.text = self.vLineTenssion;
        }
        
    }
}

- (NSString *)disposeRactetBuyTimeWith:(NSString *)buyTime {
    
    if (buyTime.length > 0) {
        NSDate *formDate = [NSDate formateYearMonthString:buyTime];
        NSDate *toDate = [NSDate formateYearMonthString:[[NSDate date] formateYearAndMonth]];
        
        BOOL ischinese = [NSBundle currentLanguageIsChinese];
        if (formDate) {
            
            NSInteger months = [NSDate numberOfMonthsWithFromDate:formDate toDate:toDate];
            
            if (months < 12) {
                months = (months <= 0 ? 1:months);
                return ischinese ? [NSString stringWithFormat:@"拍龄: %zd 个月",months] : [NSString stringWithFormat:@"Age: %zd months",months];
            }else {
                months = (months <= 0 ? 1:months);
                NSInteger years = months / 12;
                return ischinese ? [NSString stringWithFormat:@"拍龄: %zd 年以上",years] :[NSString stringWithFormat:@"Age: More than %zd years",years];
            }
        }else{
            return @"";
        }
    }
    return @"";
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    CGFloat ractetH = self.RacketView.bounds.size.height;
    CGFloat ractetImageH = self.racketImageView.bounds.size.height;
    
    self.racketImageView.layer.borderWidth = 1.5;
    self.racketImageView.layer.borderColor = [UIColor whiteColor].CGColor;
    self.RacketView.layer.borderWidth = 1.5;
    self.RacketView.layer.borderColor = [UIColor colorWithHex:0x83f09f].CGColor;
    self.racketImageView.layer.cornerRadius = ractetImageH * 0.5;
    self.RacketView.layer.cornerRadius = ractetH * 0.5;
    self.RacketView.layer.masksToBounds = YES;
    self.racketImageView.layer.masksToBounds = YES;
    
    self.VLineLabel.text = [NSBundle currentLanguageIsChinese] ? @"竖线":@"Main\nStrings";
    self.hLineLabel.text = [NSBundle currentLanguageIsChinese] ? @"横线":@"Cross\nStrings";
    
    self.racketImageView.userInteractionEnabled = YES;
    [self.racketImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(setHeadIcon)]];
}
- (void)setHeadIcon {
    [self.racketImageView showImage];
}
- (void)setVerticalLine:(NSString *)VerticalLine {
    _VerticalLine = VerticalLine;
    
    NSArray *array = [VerticalLine componentsSeparatedByString:@","];
    
    if (array.count >= 4) {
        self.vLineBrand = array[0];
        self.vLineModel = array[1];
        self.vLineSize = array[2];
        self.vLineTenssion = array[3];
    }
    
}
- (void)setLine:(NSString *)Line {
    _Line = Line;
    NSArray *array = [Line componentsSeparatedByString:@","];
    if (array.count >= 4) {
        self.hLineBrand = array[0];
        self.hLineModel = array[1];
        self.hLineSize = array[2];
        self.hLineTenssion = array[3];
    }
}

- (NSString *)vLineSize {
    if (_vLineSize == nil) {
        return @"";
    }
    return _vLineSize;
}
- (NSString *)vLineTenssion {
    if (_vLineTenssion == nil) {
        return @"";
    }
    return _vLineTenssion;
}
- (NSString *)vLineBrand {
    if (_vLineBrand == nil) {
        return @"";
    }
    return _vLineBrand;
}
- (NSString *)vLineModel {
    if (_vLineModel == nil) {
        return @"";
    }
    return _vLineModel;
}
- (NSString *)hLineSize {
    if (_hLineSize == nil) {
        return @"";
    }
    return _hLineSize;
}
- (NSString *)hLineTenssion {
    if (_hLineTenssion == nil) {
        return @"";
    }
    return _hLineTenssion;
}
- (NSString *)hLineBrand {
    if (_hLineBrand == nil) {
        return @"";
    }
    return _hLineBrand;
}
- (NSString *)hLineModel {
    if (_hLineModel == nil) {
        return @"";
    }
    return _hLineModel;
}

@end
